/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.network;

import client.logic.ClientLogicImplement;
import client.meta.FileSlot;
import clientside.DataClientSide;
import com.cse.p2p.common.CommonSettings;
import com.cse.p2p.common.DicFile;
import com.cse.p2p.database.ClientDatabase;
import com.cse.p2p.localfile.LocalFile;
import com.cse.p2p.parser.PeerInfo;
import java.io.*;
import java.net.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author TuyetNgan
 */
public class ClientRequestDownloadFile extends Thread {

	private String machine;
	private int portControl;
	private Socket clientSocket = null;
	private BufferedReader infromScreen = null;
	private BufferedReader in = null;
	private PrintStream out = null;
	private String IPconnect;
	private DicFile dicFile;
	private String userPath;
	private FileSlot fileSlot;
	private DataClientSide dataThread;
	private ClientControlTask_Client callServer;

	/**
	 * This constructor is deprecated and replaced by 
	 * {@link ClientRequestDownloadFile#ClientRequestDownloadFile(com.cse.p2p.common.DicFile, java.lang.String, client.meta.FileSlot) }
	 * 
	 * 
	 * @see #ClientRequestDownloadFile(com.cse.p2p.common.DicFile, java.lang.String, client.meta.FileSlot) 
	 * @param dic
	 * @param path
	 * @deprecated 
	 */
	public ClientRequestDownloadFile( DicFile dic, String path ) {
		this.dicFile = dic;
		this.userPath = path;
	}

	/**
	 * 
	 * @param dic	
	 * @param path	The absolute path to the location the user want to store new
	 * file (<b>including</b> the file name)
	 * @param fs	The {@link FileSlot} object that link to this thread
	 */
	public ClientRequestDownloadFile( DicFile dic, String path, FileSlot fs, DataClientSide dataThread ) {
		this.dicFile = dic;
		this.userPath = path;
		this.fileSlot = fs;
		this.dataThread = dataThread;
	}

	@Override
	public void run() {
		/* check if all pieces of the file is exist*/
		try {
			int[] neededPieces = ClientLogicImplement.neededPieces( dicFile );
			if ( neededPieces.length < 1 ) {
				LocalFile.concatenatePiecesToFileAndCopyToUserpath( CommonSettings.DEFAULT_TEMP_FOLDER + File.separatorChar + dicFile.getFileID(), userPath );
				this.fileSlot.setStatus( "Completed");
				return;
			}
			/* request server */
			callServer = new ClientControlTask_Client( dicFile.getTrackerURL(),
													   CommonSettings.SERVER_SERVERSOCKET,
													   dicFile,
													   null, dataThread );
			callServer.start();
			/* wait for receive all piece */
			while ( !callServer.getisReceived() );
			/* concatenate all piece in LocalFile */
			neededPieces = ClientLogicImplement.neededPieces( dicFile );
			if ( neededPieces.length < 1 ) {
				LocalFile.concatenatePiecesToFileAndCopyToUserpath( CommonSettings.DEFAULT_TEMP_FOLDER + File.separatorChar + dicFile.getFileID(), userPath );
				this.fileSlot.setStatus( "Completed");
				return;
			}
		}
		catch ( Exception e ) {
			JOptionPane.showMessageDialog( null, "There is an error with your download", "Download error", JOptionPane.ERROR_MESSAGE );
			e.printStackTrace();
		}

	}

	public void requestStop() {
		callServer.requestStop();
		interrupt();
	}
}
